package com.youkeda.wacai.web.control;

import com.youkeda.wacai.web.model.*;
import com.youkeda.wacai.web.service.FinanceService;
import com.youkeda.wacai.web.service.RecordService;
import com.youkeda.wacai.web.service.impl.JdFinanceServiceImpl;
import com.youkeda.wacai.web.service.impl.RecordServiceImpl;
import com.youkeda.wacai.web.service.impl.YuebaoFinanceServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class AccountingController{
    private static final List<AccountingRecord> records = new ArrayList<>();
    private static final List<Payinfo> payinfos = new ArrayList<Payinfo>();
    private static RecordService recordService = new RecordServiceImpl();

    //计算余额
@RequestMapping(path = "/accounting")
    public Accounting accounting(Accounting accounting){
        int result =accounting.getCash()+ accounting.getIncome()-
                accounting.getRent()- accounting.getCharges()-
                accounting.getEat()- accounting.getTreat()- accounting.getKtv();
        accounting.setBalance(result);
        return accounting;
    }

    //记账

@RequestMapping(path = "/record")
    public String record(AccountingRecord accountingRecord){
        if (accountingRecord.getAmount() == 0){
            return  "";
        }
        Date time = new Date();
        accountingRecord.setTime(time);
        //调用方法
        recordService.record(accountingRecord);
        return "记录成功";
       /* String result="";
        for(AccountingRecord record1:records){
            result = result + "发生日期" + record1.getCreateTime() + "记录时间" + record1.getTime() +
                     "金额:" + record1.getAmount() + "类型:" + record1.getType() +
                    "种类:" + record1.getCategory();
            result = result + "</br>";
        }
        return result;*/
    }

    @RequestMapping(path = "/query")
    public List<AccountingRecord> query(){
        return recordService.query();
    }

    //过滤数据
    @RequestMapping(path = "/search")
    public String search(@RequestParam("amount") int amount) {
        List<AccountingRecord> filterd = records.stream().filter(str -> str.getAmount() > amount).
                collect(Collectors.toList());
        StringBuilder sb = new StringBuilder();
        for(AccountingRecord record2:filterd){
            sb.append("发生日期" + record2.getCreateTime() + "记录时间" + record2.getTime() +
                    "金额:" + record2.getAmount() + "类型:" + record2.getType() +
                    "种类:" + record2.getCategory() + "</br>" );
        }
        return sb.toString();
    }

    //数据初始化

    @PostConstruct
    public void init(){
        //初始化白条数据
        Payinfo baitiao = new Payinfo();
        baitiao.setPayType(PayType.baitiao);
        baitiao.setBillingDate(10);
        baitiao.setDueDate(20);
        payinfos.add(baitiao);
        //初始化花呗数据
        Payinfo huabei = new Payinfo();
        huabei.setPayType(PayType.huabei);
        huabei.setBillingDate(20);
        huabei.setDueDate(10);
        payinfos.add(huabei);
        //初始化信用卡数据
        CreditCard creditCard = new CreditCard();
        creditCard.setPayType(PayType.creditCard);
        creditCard.setBillingDate(5);
        creditCard.setDueDate(25);
        creditCard.setName("招商银行信用卡");
        creditCard.setCardNumber("111111111111");
        payinfos.add(creditCard);
    }

    //支付方式
    @RequestMapping(path="/pay")
    public Payinfo pay(@RequestParam("amount")double amount,
                       @RequestParam("payType") PayType payType,
                       @RequestParam("stagesCount")int stagesCount){
        List<Payinfo>payinfoList = payinfos.stream().filter(payinfo1 ->payinfo1.getPayType().
                equals(payType)).collect(Collectors.toList());
        if (payType.equals(PayType.creditCard)){
            CreditCard creditCard = (CreditCard) payinfoList.get(0);
            //返回数据
            CreditCard result = new CreditCard();
            result.setBillingDate(creditCard.getBillingDate());
            result.setDueDate(creditCard.getDueDate());
            result.setPayType(payType);
            result.setAmont(amount);
            result.setStagesCount(stagesCount);
            result.setName(creditCard.getName());
            result.setCardNumber(creditCard.getCardNumber());
            return result;
        }else {
            Payinfo payinfo = payinfoList.get(0);
            //返回数据
            Payinfo result = new Payinfo();
            result.setBillingDate(payinfo.getBillingDate());
            result.setDueDate(payinfo.getDueDate());
            result.setPayType(payType);
            result.setAmont(amount);
            result.setStagesCount(stagesCount);
            return result;
        }

    }

    //计算收益
    @RequestMapping(path = "/finance")
    public FinanceInfo finance(@RequestParam("type") FinanceType type,
                               @RequestParam("amount") double amount,
                               @RequestParam("days") int days){
        FinanceService financeService = null;
        //根据不同类型，实例化不同的服务实现者
        if (FinanceType.yuebao.equals(type)){
            financeService = new YuebaoFinanceServiceImpl();
        }else if(FinanceType.jd.equals(type)){
            financeService = new JdFinanceServiceImpl();
        }else {
            return  null;
        }
        return financeService.invest(amount,days);
    }
}
